function add_comment(data)
{
	var template = $('#commentTpl').html();
	var html = Mustache.to_html(template,data);
	$('#feeds_comment_'+data.feeds_id).append(html).hide().fadeIn('slow');
}

function status_update(comments,html,data)
{
	 var template = $('#postTpl').html();
 	 var data = {
 	 	comments:comments,
 	 	html:html,
 	 	data:data	
 	 }
    var html = Mustache.to_html(template, data);
	$(html).prependTo('#postContainer').hide().fadeIn('slow');
}


$(document).ready(function(){
	$('#postLoading').hide();
	var pusher = new Pusher(PUSHER_APP_KEY);
	var channel = pusher.subscribe('my-channel');
	
	channel.bind('comment-added', function(data) {
	 	add_comment(data);
	 	jQuery("abbr.timeago").timeago();
	});

	channel.bind('status-update', function(data) {

		var user_id = G_USER_ID;
		if(user_id == data.user_id){
			console.log(data);
			status_update(data.feed_title,data.feed_container,data);
			jQuery("abbr.timeago").timeago();
		}
	});

	channel.bind('likes-update', function(data) {
		var current = SS_USER_ID;
		$('#feeds_total_likes_'+data.feed_id).text(data.total_likes + ' People like this');
		if(current == data.user_id){
			if(data.type == 'like')
			{
				$('#feed_like_id_'+data.feed_id).text('Unlike');

			}
			else if(data.type ="unlike")
			{
				$('#feed_like_id_'+data.feed_id).text('Like');
			}
		}
		
	});

});


jQuery(document).ready(function() {
	jQuery("abbr.timeago").timeago();
});

$(document).on("click",".btnLike",function(){
	var feed_id = $(this).attr('feed-like-id');
	var text = $(this).text();
	if(text == "Like")
	{
		$(this).text('Unlike');
		$.ajax({
			url: URL+'likes/insert',
			data: {feed_id:feed_id},
			type: 'post',
			dataType: 'json',
			success: function(result){
				if(result.status == true)
				{
					
				}
				else
				{
					
				}
			}
		})
	}
	else if(text == 'Unlike')
	{
		$(this).text('Like');
		$.ajax({
			url: URL+'likes/delete',
			data: {feed_id:feed_id},
			type: 'post',
			dataType: 'json',
			success: function(result){
				if(result.status == true)
				{
			
				}
				else
				{
					
				}
			}
		})	
	}
	
});

